/*
 * Copyright (C) 2009-2018 Hangzhou 2Dfire Technology Co., Ltd. All rights reserved
 */
package com.ws.soa.consumer.controller;

import com.google.common.base.Preconditions;
import com.ws.soa.consumer.domain.WorksDO;
import com.ws.soa.consumer.dto.WorkCommentDTO;
import com.ws.soa.consumer.dto.WorkInsertDTO;
import com.ws.soa.consumer.service.SessionStorageClientService;
import com.ws.soa.consumer.service.WorksCommentService;
import com.ws.soa.consumer.service.WorksLaudService;
import com.ws.soa.consumer.service.WorksService;
import com.ws.soa.consumer.session.UserSessionStorage;
import com.ws.soa.consumer.vo.WorkCommentVO;
import com.ws.soa.consumer.vo.WorkVO;
import com.ws.soa.consumer.vo.WorksVO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * WorkController
 *
 * @author luobosi@2dfire.com
 * @since 2019-02-18
 */
@RestController
@RequestMapping(("/work"))
public class WorkController {

  @Resource
  private WorksService worksService;

  @Resource
  private WorksCommentService worksCommentService;
  @Resource
  private WorksLaudService worksLaudService;
  @Resource
  private SessionStorageClientService sessionStorageClientService;

  /**
   * 作品上传
   *
   * @return true/false
   */
  @PostMapping(value = "/insert")
  public Boolean insertWork(HttpServletRequest request, @RequestBody WorkInsertDTO workInsertDTO) {
    Preconditions.checkArgument(null != workInsertDTO);

    UserSessionStorage
        userSessionStorage =
        this.sessionStorageClientService.getUserSessionStorage(request);
    return this.worksService.save(userSessionStorage.getMemberId(), workInsertDTO);
  }


  /**
   * 添加作品评论
   *
   * @param workCommentDTO 评论的内容
   * @return true/false
   */
  @PostMapping(value = "/comment")
  public Boolean insertWorkComment(HttpServletRequest request,
                                   @RequestBody WorkCommentDTO workCommentDTO) {
    Preconditions.checkArgument(null != workCommentDTO);

    UserSessionStorage userSessionStorage =
        sessionStorageClientService.getUserSessionStorage(request);

    return this.worksCommentService
        .save(userSessionStorage.getMemberId(), userSessionStorage.getNickName(), workCommentDTO);
  }

  /**
   * 给某个作品点赞
   *
   * @param workId 作品ID
   * @return 作品评论列表
   */
  @GetMapping(value = "/laud")
  public Boolean workLaud(HttpServletRequest request,
                          @RequestParam(name = "work_id") String workId) {
    UserSessionStorage userSessionStorage =
        sessionStorageClientService.getUserSessionStorage(request);
    return this.worksLaudService.save(userSessionStorage.getMemberId(), workId);
  }

  /**
   * 用户某个作品的评价列表
   *
   * @param workId 作品ID
   * @return 作品评论列表
   */
  @GetMapping(value = "/comment_list")
  public WorkCommentVO commentList(@RequestParam(name = "work_id") String workId) {
    return this.worksCommentService.findAllByWorkId(workId);
  }

  /**
   * 获取的作品列表
   *
   * @return 作品评论列表
   */
  @GetMapping(value = "/work_list")
  public List<WorkVO> workList(HttpServletRequest request) {
    UserSessionStorage userSessionStorage =
        this.sessionStorageClientService.getUserSessionStorage(request);
    return this.worksService.findAll(userSessionStorage.getMemberId());
  }


  /**
   * 获取已购作品
   *
   * @return 作品评论列表
   */
  @GetMapping(value = "/purchased_work_list")
  public List<WorksVO> purchasedWorkList(HttpServletRequest request) {
    UserSessionStorage userSessionStorage =
        this.sessionStorageClientService.getUserSessionStorage(request);

    return null;
  }

}
